package com.qf.consumer.ps;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConsumerMain {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("cdm");

        Connection conn = factory.newConnection();
        Channel channel = conn.createChannel();


        //如果rabbitmq服务端没有这个名称的队列，创建。如果有，不创建
        channel.basicConsume("ps.que1",true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("consumerTag:" + consumerTag);
                System.out.println("deliveryTag:" + envelope.getDeliveryTag());
                System.out.println("body:" + new String(body,"utf-8"));

                System.out.println("--------------------------------");
            }
        });
    }
}
